/**
 * Created by  on 2017/8/30.
 */

var ProductChecker = {

    currentViewProductId: 0,//当前显示品牌信息Id

    /**
     * 加载品牌详情信息
     * @param id
     */
    loadProductDetail: function (id) {
        ProductChecker.currentViewProductId = id;

        var selector = "#page_product_detail";

        function callback(data, status, xhr) {
            if (status == "success") {
                $(selector).empty();
                $(selector).html(data);
                ProductChecker.showProductDetail();
            } else {
                $(selector).html("错误信息: <br/>" + xhr.status + " " + xhr.statusText)
            }
        }

        var url = "/smAdmin/check/product_detail?id=" + id;
        // 请求编辑框的页面数据库
        $.get(url, "", callback);
    },

    /**
     * 显示品牌列表信息
     */
    showPageProductList: function () {
        $('#page_product_list').show();
        $('#page_product_detail').hide();
        ProductChecker.reloadProductList();
    },

    /**
     * 显示品牌详情信息
     */
    showProductDetail: function () {
        $('#page_product_list').hide();
        $('#page_product_detail').show();
    },

    /**
     * 重新加载品牌列表信息
     */
    reloadProductList: function () {
        var filters = {"groupOp": "AND", "rules": []};
        var rules = filters.rules;
        var args = formserializeJson("product_list_form");
        for (var name in args) {
            var value = args[name];
            if (value) {
                rules.push({field: name, op: "cn", data: value});
            }
        }

        var postData = $("#product-grid-table").jqGrid("getGridParam", "postData");
        $.extend(postData, {filters: JSON.stringify(filters)});
        $("#product-grid-table").jqGrid("setGridParam", {search: true}).trigger("reloadGrid", [{page: 1}]);  //重载JQGrid
    },

    /**
     * 显示输入品牌审核信息兑换框
     */
    showCheckProductDialog: function () {
        var layer = layui.layer;
        var that = this;
        //多窗口模式，层叠置顶
        layer.open({
            type: 1
            , title: '请输入审核备注信息：'
            , area: ['500px', '350px']
            , shade: 0
            , maxmin: true
            , offset: "auto"
            , content: '<div class="alert alert-info bigger-110"><textarea id="Product_check_mark_content" style="width: 100%;height: 200px" maxlength="500"></textarea></div>'
            , btn: ['驳回', '通过']
            , yes: function () {
                var check_mark = $("#product_check_mark_content").val();
                ProductChecker.submitProductCheckResult(0, check_mark);
                // layer.closeAll();
            }
            , btn2: function () {
                var check_mark = $("#product_check_mark_content").val();
                ProductChecker.submitProductCheckResult(1, check_mark);
                // layer.closeAll();
                return false;
            }
            , zIndex: layer.zIndex //重点1
            , success: function (layero) {
                layer.setTop(layero); //重点2
            }
        });
    },

    /**
     * 提交审核结果
     * @param is_pass 是否审核通过 1-表示通过，0-表示驳回
     * @param check_mark 审核备注信息
     */
    submitProductCheckResult: function (is_pass, check_mark) {

        function callback(data, status, xhr) {
            if (status == "success") {
                if (data.c == 0) {
                    ProductChecker.loadProductDetail(ProductChecker.currentViewProductId);
                    layui.layer.closeAll();
                    alert("成功：" + data.m);
                } else {
                    alert("错误：" + data.m);
                }
            } else {
                var msg = "错误信息: <br/>" + xhr.status + " " + xhr.statusText;
                alert(msg);
            }
        }

        var url = "/smAdmin/check/product_submit_check_result";
        var post_data = {
            id: ProductChecker.currentViewProductId,
            check_mark: check_mark,
            is_pass: is_pass
        };
        $.post(url, post_data, callback, 'json');
    },

    // 创建新对象
    createNew: function () {
        var instance = {};

        return instance;
    },

};